//
//  ANMineTicketController.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANMineTicketController.h"
#import "ANBasePageViewController.h"
#import "TicketCell.h"
#import "TicketEntity.h"
#import "ANMineTickerTopView.h"

#import "ANMineAllTicketController.h"
#import "ANMineUseTicketController.h"

@interface ANMineTicketController ()<ANBasePageViewControllerDelegate,ANBasePageViewControllerDataSource,ANMineTickerTopViewDelegate>

@property (nonatomic, strong) ANMineTickerTopView *topView;
@property (nonatomic, strong) ANBasePageViewController *pageVc;


@end

@implementation ANMineTicketController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的优惠券";
    
    [self requestData];
}

- (void)setupSubViews
{
    self.topView = [[ANMineTickerTopView alloc] init];
    self.topView.delegate = self;
    self.pageVc = [[ANBasePageViewController alloc] init];
    [self addChildViewController:self.pageVc];
    self.pageVc.delegate =  self;
    self.pageVc.dataSource = self;
}

- (void)setupConstraints {
    
    [self.view addSubview:self.topView];
    [self.view addSubview:self.pageVc.view];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.equalTo(@50);
    }];
    
    [self.pageVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(50);
    }];
    [self.pageVc reloadPageViewController];
}

- (void)requestData {
    
    [URLManager.shareManager getUrlStr:Api_Mine_Tickets_Num parameters:@{@"memberId": UserUtils.shared.vipId} success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                self.topView.couponNum = [responseObj[@"data"][@"couponNum"] intValue];
                self.topView.useNum = [responseObj[@"data"][@"useNum"] intValue];
            }
        }
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - ANMineTickerTopViewDelegate
- (void)ANMineTickerTopView:(ANMineTickerTopView *)searchView didSelectedIndex:(NSInteger)index lastSelectedIndex:(NSInteger)lastIndex
{
    if (index > lastIndex) {
        [self.pageVc setViewControllerIndex:index direction:ANBasePageViewControllerDirectionForward animated:YES];
    }
    if (index < lastIndex) {
        [self.pageVc setViewControllerIndex:index direction:ANBasePageViewControllerDirectionReverse animated:YES];
    }
}


#pragma mark - ANBasePageViewControllerDelegate,ANBasePageViewControllerDataSource

///返回当前显示的视图控制器
- (void)basePageViewController:(ANBasePageViewController *)pageVC didFinishScrollWithCurrentViewController:(UIViewController *)viewController
{
    NSInteger index = 0;
    if ([viewController isKindOfClass:[ANMineAllTicketController class]]) {
        index = 0;
    }
    if ([viewController isKindOfClass:[ANMineUseTicketController class]]) {
        index = 1;
    }
    [self.topView setSelectedIndex:index];
}

///返回当前将要滑动的视图控制器
-(void)basePageViewController:(ANBasePageViewController *)pageVC willScrollerWithCurrentViewController:(UIViewController *)ViewController
{
    
}

#pragma mark - ANBasePageViewControllerDataSource
- (NSInteger)numberViewControllersInBasePageViewController:(ANBasePageViewController *)pageVC
{
    return 3;
}

- (UIViewController *)basePageViewController:(ANBasePageViewController *)pageVC indexOfViewControllers:(NSInteger)index
{
    if (index == 0) {
        ANMineAllTicketController *vc = [[ANMineAllTicketController alloc] init];
        return vc;
    }
    else {
        ANMineUseTicketController *vc = [[ANMineUseTicketController alloc] init];
        return vc;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
